package com.hehao.java_basic.design_mode.creational.builder;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class SingletonDemo {
    private static volatile SingletonDemo instance;

    private SingletonDemo() {
    }

    public static SingletonDemo getInstance() {
        if (instance == null) {
            synchronized (SingletonDemo.class) {
                if (instance == null) {
                    instance = new SingletonDemo();
                }
            }
        }
        return instance;
    }

    public void doSomething() {
        System.out.println("Doing something in SingletonDemo");
    }

    public static void main(String[] args) {
        int threadCount = 1000; // 并发线程数
        ExecutorService executorService = Executors.newFixedThreadPool(threadCount);

        for (int i = 0; i < threadCount; i++) {
            executorService.execute(() -> {
                SingletonDemo singleton = SingletonDemo.getInstance();
                singleton.doSomething();
            });
        }

        executorService.shutdown();
    }
}